/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLCharMarker.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


PGL_IMPLEMENT_DYNCREATE(CPGLCharMarker,CPGLMarker);

#ifdef _DEBUG
void CPGLCharMarker::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLMarker::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("--- CPGLCharMarker") << GetID() <<_T(" ---\n");
}

void CPGLCharMarker::AssertValid() const
{
    // call inherited AssertValid first
    CPGLMarker::AssertValid();
} 
#endif

void CPGLCharMarker::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLCharMarker");

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLMarker::SerializeXML(ar);

			xml.AddChildElemEx( _T("Text"), m_sText);
			xml.AddChildElemEx(_T("Font"));
			xml.IntoElem();
				m_font.SerializeXML( ar );
			xml.OutOfElem();

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// loading base class
			CPGLMarker::SerializeXML(ar);

			xml.FindGetChildData( _T("Text"), m_sText);
			
			xml.ResetChildPos();
			if (xml.FindChildElem( _T("Font")) )
			{
				xml.IntoElem();
					m_font.SerializeXML( ar );
				xml.OutOfElem();
			}

		xml.OutOfElem();
	}
}

CPGLCharMarker::CPGLCharMarker(const CPGLColor& _color, const CPGLColor& _FillColor, double dRadius, bool bFilled, LPCTSTR szString)
:CPGLMarker( _color, _FillColor, dRadius, bFilled ), 
m_sText(szString)
{
	LOGFONT lf = m_font.GetLOGFONT();

	lf.lfHeight=(long)floor(2*GetRadius());
	m_font.SetLOGFONT(lf);
}

CPGLCharMarker& CPGLCharMarker::operator = (const CPGLCharMarker& c)
{
	if (this != &c)
	{
		this->CPGLMarker::operator =(c);

		m_sText = c.m_sText;
		m_font.SetLOGFONT(c.m_font.GetLOGFONT());
	}

	return *this;
}

void CPGLCharMarker::PlotGfx(gfxinterface::CGfxInterface& gfx, CPGLDataPtr pDataX, CPGLDataPtr pDataY)
{
	using namespace gfxinterface;
	if (!pDataX || !pDataY || !IsVisible())
		return;

	const UINT n = __min( pDataX->size(), pDataY->size());

	if (n==0)
		return;

	// drawing....
	CPGLMarker::PlotGfx(gfx, pDataX, pDataY);

	std::vector<std::_tstring> vStrings(n);
	std::vector<gfxinterface::SPointD> vPoints(n);

	// gettin size of character...
	SPointD p=gfx.MeasureString(m_sText,m_font.GetLOGFONT());
	double dy=gfx.BBToWorldAbsy(p.y*0.5);

	for (UINT i=0;i<n;i++)
	{
		vPoints[i].x=(*pDataX)[i];
		vPoints[i].y=(*pDataY)[i]-dy;
		vStrings[i]=m_sText;
	}

	m_font.SetFontGfx(gfx);
	gfx.DrawTextStrip( vPoints, vStrings, gfxinterface::TextAlignmentCenter);
}

void CPGLCharMarker::PlotLegendGfx(gfxinterface::CGfxInterface& gfx, float x, float y)
{
	if (!IsVisible())
		return;

	CPGLMarker::PlotLegendGfx(gfx, x,y);

	m_font.SetFontGfx(gfx);
	gfx.DrawText( x, y-gfx.BBToWorldAbsy(2.1*GetRadius()), 
		m_sText, gfxinterface::TextAlignmentCenter);
}

void CPGLCharMarker::SetRadius(double radius)
{
	CPGLMarker::SetRadius(radius);

	LOGFONT lf = m_font.GetLOGFONT();

	lf.lfHeight=(long)floor(2*GetRadius());
	m_font.SetLOGFONT(lf);
}
